package it.univaq.di.helpdesk.business.common.impl.ibatis;

import it.univaq.di.helpdesk.business.common.FaqService;
import it.univaq.di.helpdesk.business.errors.BusinessException;
import it.univaq.di.helpdesk.business.model.Faq;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;

public class IBATISFaqService implements FaqService {
	private SqlMapClient sqlMap;
	
	public IBATISFaqService(SqlMapClient sqlMap) {
		super();
		this.sqlMap = sqlMap;
	}

	@Override
	public void addFaq(Faq faq) throws BusinessException {
		try {
			sqlMap.insert("addFaq",faq);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
	}

	@Override
	public List<Faq> findAllFaq() throws BusinessException {
		List<Faq> faqs = null;
		
		try {
			faqs = (List<Faq>) sqlMap.queryForList("getFaq");
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return faqs;
	}

	@Override
	public Faq findFaqById(int ID) throws BusinessException {
		Faq faq = null;
		
		try {
			faq = (Faq) sqlMap.queryForObject("findFaqById", ID);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		
		return faq;
	}
	
	@Override
	public Faq getFaqById(int ID) throws BusinessException {
		Faq faq = null;
		
		try {
			faq = (Faq) sqlMap.queryForObject("Faq.getFaqByID", ID);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		
		return faq;
	}

	@Override
	public void updateFaq(Faq faq) throws BusinessException {
		try {
			sqlMap.update("editFaq", faq);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
	}

	@Override
	public List<Faq> searchForFaqs(Map<String, String> queryParam)
			throws BusinessException {
		List<Faq> faqs = null;
		
		try {
			faqs = (List<Faq>) sqlMap.queryForList("searchForFaqs", queryParam);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return faqs;
	}

	@Override
	public void deleteFaq(int faqID) throws BusinessException {
		try {
			sqlMap.delete("deleteFaq", faqID);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		
	}
}
